class FieldValidator{
    constructor(txtId,validaFun){
        this.input = $('#'+txtId);
        //保存一下函数
        this.validaFun = validaFun
        this.p = this.input.nextElementSibling;
        //假如表单失去焦点的时候，就会触发验证
        this.input.onblur = ()=>{
            this.validator()
        }

    }
    // 此函数为验证，成功返回ture，失败返回flase
    async validator(){
        // 这里收取到验证信息是否有误
        const err =  await this.validaFun(this.input.value)
        // 假如确实收到了错误信息，那么就把错误信息给返回
        if(err){
            this.p.innerText = err
            return false
        }else{
            this.p.innerText = ''
            return true
        }
    }

    static async validator(...validators){
        const resp =  validators.map((a)=>a.validator());
        const result = await Promise.all(resp);
        return result.every((a)=>a)
    }
}






